var deptBorderEdit = {
	grid:null,
	centerMarker:null,
	areayPolgon:null,
	drawingManager:null,
	init: function(){
		var boss = this;
		boss.fix();
		boss.initLayout();
		boss.initBaseMap();
		boss.initEvent();
		boss.renderBlackBorder();
		boss.renderBorder();
	},
	initBaseMap:function(){
		var boss = this;
		boss.map = new BMap.Map("bmap");    
		boss.map.centerAndZoom(new BMap.Point(113.895367, 22.56343), 15);
		boss.map.addControl(new BMap.MapTypeControl());
		boss.map.enableScrollWheelZoom();
		
		boss.drawingManager = new BMapLib.DrawingManager(boss.map, {
	        isOpen: false, //是否开启绘制模式
	    });  
		boss.drawingManager.addEventListener('overlaycomplete', boss.setMapxy);
		
		var myZoomCtrl = new MyMapControl(BMAP_ANCHOR_TOP_LEFT,20, 40,"<div id='editPanel'></div>");
		boss.map.addControl(myZoomCtrl);
		
		$("#editPanel").html($("#modelDetail").html());
		$("#modelDetail").remove();
		
		$("#editPanel").removeAttr("style");
	},
	initLayout: function(){
		
	},
	initEvent: function(){
		var boss = this;
		$("#resetCenterPoint").bind("click",function(){
			boss.clearMapEvent();
			deptBorderEdit.map.addEventListener("click",deptBorderEdit.setCenterPoint);
			deptBorderEdit.map.addEventListener("mousemove",deptBorderEdit.moveMarker);
		});
		
		$("#editBorder").bind("click",function(){
			boss.clearMapEvent();
			var start = $(this).attr("start");
			if(start == "false"){
				boss.areayPolgon.enableEditing();
				$(this).attr("start","true");
				$(this).html("保存边界");
			}
			else{
				boss.areayPolgon.disableEditing();
				$(this).attr("start","false");
				$(this).html("编辑边界");
				boss.setBorder();
			}
		});
		
		$("#drawBtn").bind("click",function(){
			boss.clearMapEvent();
			boss.drawingManager.open();
			boss.drawingManager.setDrawingMode(BMAP_DRAWING_POLYGON);
		});
		
		$("#saveBtn").bind("click",function(){
			var data = sbl.getFormData("editPanel", false);
			sbl.ajax({
				url:burl+"/Dept/update.do",
				data:data,
				success:function(){
					alert("更新成功。");
					window.close();
				}
			});
		});
		
		$("#submitBtn").bind("click",function(){
			var data = sbl.getFormData("editPanel", false);
			sbl.ajax({
				url:burl+"/Dept/insert.do",
				data:data,
				success:function(){
					alert("提交成功。");
					window.close();
				}
			});
		});
	},
	clearMapEvent:function(){
		deptBorderEdit.map.removeEventListener("mousemove",deptBorderEdit.moveMarker);
		deptBorderEdit.map.removeEventListener("click",deptBorderEdit.setCenterPoint);
		deptBorderEdit.areayPolgon.disableEditing();
	},
	moveMarker:function(e){
		deptBorderEdit.centerMarker.setPosition(e.point);
	},
	setCenterPoint:function(e){
		deptBorderEdit.clearMapEvent();
		var mapx = e.point.lng;
		var mapy = e.point.lat;
		$("#mapx").val(mapx);
		$("#mapy").val(mapy);
	},
	setMapxy:function(e){
		deptBorderEdit.areayPolgon = e.overlay;
		deptBorderEdit.setBorder();
		deptBorderEdit.drawingManager.close();
	},
	setBorder:function(){
		var boss = this;
		var points = boss.areayPolgon.getVertexPoints();
		var pointstr = "";
		for(var i=0;i<points.length;i++){
			var point = points[i];
			pointstr += (point.p.lng+","+point.p.lat);
			if(i < points.length -1){
				pointstr += ";";
			}
		}
		$("#mapxy").val(pointstr);
	},
	fix:function(isOpen){
		var bodyWidth = document.documentElement.clientWidth;
		var bodyHeight = document.documentElement.clientHeight;
		$("body").css("width",bodyWidth);
		$("body").css("height",bodyHeight);
	},
	renderBorder:function(){
		var boss = this;
		var mapxy = $("#mapxy").val();
		var mapx = $("#mapx").val();
		var mapy = $("#mapy").val();
		var fillColor = "";
		var centerPoint = new BMap.Point(mapx,mapy);
		var option={fillColor:fillColor,strokeWidth:"5px",fillOpacity:0.4,strokeColor:"red"};
		boss.areayPolgon = boss.getPolygon(mapxy,option);
		boss.map.addOverlay(boss.areayPolgon);
		
		boss.centerMarker = new BMap.Marker(centerPoint);
		boss.map.addOverlay(boss.centerMarker);
		
		if(!boss.map.getBounds().containsPoint(centerPoint))
		{
			boss.map.setCenter(centerPoint);
		}
	},
	renderBlackBorder:function(){
		var boss = this;
		var mapxys = $("#blackDepts input[name='blackdepts']");
		var option={fillColor:"",strokeWidth:"5px",fillOpacity:0.4,strokeStyle:"dashed",strokeColor:"blue"};
		for(var i=0;i<mapxys.length;i++){
			var mapxy = $(mapxys[i]).val();
			var polgon = boss.getPolygon(mapxy,option);
			boss.map.addOverlay(polgon);
		}
	},
	getPolygon:function(mapxy,option){
		var boss = this;
		var points = [];
		var lonlats = mapxy.split(";");
		
		for(var j =0;j<lonlats.length;j++){
			var lonlat = lonlats[j].split(",");
			var lon = lonlat[0];
			var lat = lonlat[1];
			var point = new BMap.Point(lon,lat);
			points.push(point);
		}
		var fillColor = "";
		
		var polygon = new BMap.Polygon(points,option);
		return polygon;
	}
};
$(function(){
	deptBorderEdit.init();
});
